import scrapy
from scrapy import Request, Selector

from bys.items import TeachinItem


class TeachinSpider(scrapy.Spider):
    """爬取宣讲会详情"""
    name = 'teachin'
    allowed_domains = ['cqu.cqbys.com']

    def start_requests(self):
        for page in range(1):
            yield Request(url=f'https://cqu.cqbys.com/teachin/index/domain/cqu/page/{page + 1}')

    def parse(self, response, **kwargs):
        sel = Selector(response)
        list_items = sel.css('ul.infoList')
        for list_item in list_items:
            status_text = list_item.css('span.status-text::text').extract_first()
            date = list_item.css('li:nth-child(3)::text').extract_first()
            if '线下' in status_text and '已取消' not in date:
                href = list_item.css('li.span9 > a::attr(href)').extract_first()
                detail_url = response.urljoin(href)
                teachin_item = TeachinItem()
                teachin_item['title'] = list_item.css('li.span9 > a::text').extract_first()
                teachin_item['date'] = date
                yield Request(url=detail_url, callback=self.parse_detail, cb_kwargs={'item': teachin_item})

    def parse_detail(self, response, **kwargs):
        sel = Selector(response)
        teachin_item = kwargs['item']
        teachin_item['browse'] = sel.css('div.operation > div.share > ul > li:nth-child(1)::text').extract_first()
        teachin_item['unit'] = sel.css('div.title-message > a.name::text').extract_first()
        teachin_item['property'] = sel.css('div.details-list > ul:nth-child(1) > li:nth-child(1) > span::text').extract_first()
        teachin_item['industry'] = sel.css('div.details-list > ul:nth-child(1) > li:nth-child(2) > span::text').extract_first()
        teachin_item['scale'] = sel.css('div.details-list > ul:nth-child(1) > li:nth-child(3) > span::text').extract_first()
        yield teachin_item
